<html>
<head><meta charset="utf-8"><title>unions, transmute, raw ptr in const fn · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html">unions, transmute, raw ptr in const fn</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240153145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240153145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240153145">(May 25 2021 at 07:46)</a>:</h4>
<p>So, now that <a href="https://github.com/rust-lang/rfcs/pull/3016">https://github.com/rust-lang/rfcs/pull/3016</a> is merged (const-UB RFC)... what is blocking allowing unions and transmute in <code>const fn</code>? (We probably should allow both of them at the same time to avoid people hackily encoding one via the other.)</p>



<a name="240153397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240153397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240153397">(May 25 2021 at 07:49)</a>:</h4>
<p>(FWIW, using <code>transmute</code> one can also deref raw ptrs by transmuting them to a reference, so I am not sure if it makes much sense to keep rejecting those)</p>



<a name="240153480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240153480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240153480">(May 25 2021 at 07:50)</a>:</h4>
<p>the main question here seems to be one of documentation... basically we need to make sure everyone understands that transmuting or otherwise converting a ptr to an int is UB during CTFE</p>



<a name="240154202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240154202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240154202">(May 25 2021 at 07:58)</a>:</h4>
<p>can we make that a compile-time error? it seems easy to have something do that accidentally</p>



<a name="240154254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240154254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240154254">(May 25 2021 at 07:59)</a>:</h4>
<p>in particular, there are a lot of valid reasons people might want to do that at compile-time -- offset_of style stuff for example.</p>



<a name="240154269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240154269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240154269">(May 25 2021 at 07:59)</a>:</h4>
<p>i think if its not a compile error, people will definitely do ptr-to-int</p>



<a name="240154464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240154464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240154464">(May 25 2021 at 08:01)</a>:</h4>
<p>It <em>could</em> be a compile error, but it's not guaranteed.</p>



<a name="240156211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240156211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240156211">(May 25 2021 at 08:22)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209168">Thom Chiovoloni</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn/near/240154202">said</a>:</p>
<blockquote>
<p>can we make that a compile-time error? it seems easy to have something do that accidentally</p>
</blockquote>
<p>yeah we'll usually detect when you do the wrong thing with a ptr-turned-integer during CTFE</p>



<a name="240156314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240156314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240156314">(May 25 2021 at 08:23)</a>:</h4>
<p>my main motivation for stabilizing unions and transmute is that they are already allowed in <code>const</code> but not in <code>const fn</code>, which is strange. I think we should keep these differences to a minimum. remaining differences (annoying promotion stuff aside) are around function pointers, which I am not touching until we make some progress on the entire "trait bounds for const fn" story.</p>



<a name="240156427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240156427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240156427">(May 25 2021 at 08:24)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209168">Thom Chiovoloni</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn/near/240154254">said</a>:</p>
<blockquote>
<p>in particular, there are a lot of valid reasons people might want to do that at compile-time -- offset_of style stuff for example.</p>
</blockquote>
<p>FWIW, <a href="https://github.com/Gilnaa/memoffset/">https://github.com/Gilnaa/memoffset/</a> does compile-time offset_of without ptr-int-casts</p>



<a name="240156467"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240156467" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240156467">(May 25 2021 at 08:24)</a>:</h4>
<p>but that needs a bunch of other unstable features</p>



<a name="240156508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240156508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240156508">(May 25 2021 at 08:25)</a>:</h4>
<p><code>#![feature(const_ptr_offset_from, const_maybe_uninit_as_ptr, const_raw_ptr_deref, const_refs_to_cell)]</code></p>



<a name="240156531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240156531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240156531">(May 25 2021 at 08:25)</a>:</h4>
<p>uh wtf, that code box shows up empty here... using inline instead of block code formatting now...</p>



<a name="240156685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240156685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240156685">(May 25 2021 at 08:27)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn/near/240156531">said</a>:</p>
<blockquote>
<p>uh wtf, that code box shows up empty here...</p>
</blockquote>
<p>I think it's because the line starts with a <code>#</code>. Try doubling it up or some other escape mechanism?</p>



<a name="240156773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240156773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240156773">(May 25 2021 at 08:28)</a>:</h4>
<p>isn't this supposed to be verbatim blocks? why would <code>#</code> make any difference? anyway, I reproted a bug and used a hack here, lets go back on topic :)</p>



<a name="240156996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240156996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240156996">(May 25 2021 at 08:30)</a>:</h4>
<p>But yeah, imo given the RFC was accepted knowing that the compiler doesn't have to guarantee a compile error, I don't see any problem doing just that. It was accepted for a reason, after all.</p>



<a name="240166377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240166377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240166377">(May 25 2021 at 10:12)</a>:</h4>
<p>We should do transmute, unions, cast and deref at the same time to reduce the hackery that ppl have to resort to</p>



<a name="240280196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240280196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240280196">(May 26 2021 at 05:05)</a>:</h4>
<p>Would someone mind pointing me to the code referenced <a href="https://github.com/rust-lang/rust/blob/47a90f452050d4ea484206447babb07dd33c21d5/library/core/src/intrinsics.rs#L1080-L1081">in this comment</a>? I'm pretty sure it's what's causing my local changes to fail to compile the tests.</p>



<a name="240280303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240280303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240280303">(May 26 2021 at 05:07)</a>:</h4>
<p>Also while going through code, I think it makes sense to stabilize all of the following at the same time:</p>
<ul>
<li><code>const_fn_transmute</code></li>
<li><code>const_fn_union</code></li>
<li><code>const_raw_ptr_deref</code></li>
<li><code>const_raw_ptr_to_usize_cast</code></li>
<li><code>const_ptr_offset</code></li>
<li><code>const_ptr_offset_from</code></li>
</ul>
<p>The first two are complementary, functionally doing the same thing. <code>const_raw_ptr_deref</code> and <code>const_raw_ptr_to_usize_cast</code> could be accomplished via a transmute. <code>const_ptr_offset</code> and <code>const_ptr_offset_from</code> could be accomplished by a transmute, arithmetic, and another transmute. To avoid writing needlessly verbose code, I think it's best to do it all at once.</p>



<a name="240429594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240429594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240429594">(May 27 2021 at 07:34)</a>:</h4>
<p><code>const_raw_ptr_to_usize_cast</code> is unlikely to be stabilized soon or ever IMO; ptr-to-int casts in CTFE are "strange"</p>



<a name="240429665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240429665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240429665">(May 27 2021 at 07:35)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn/near/240166377">said</a>:</p>
<blockquote>
<p>We should do transmute, unions, cast and deref at the same time to reduce the hackery that ppl have to resort to</p>
</blockquote>
<p>hm, I feel we might separate the raw ptr stuff from the rest -- we already have that sitaution in <code>const</code>  items (transmute allowed, raw ptr deref not)</p>



<a name="240429689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240429689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240429689">(May 27 2021 at 07:35)</a>:</h4>
<p>so I don't think we make things worse here by stabilizing only transmute and union</p>



<a name="240429783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240429783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240429783">(May 27 2021 at 07:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="245610">Jacob Pratt</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn/near/240280196">said</a>:</p>
<blockquote>
<p>Would someone mind pointing me to the code referenced <a href="https://github.com/rust-lang/rust/blob/47a90f452050d4ea484206447babb07dd33c21d5/library/core/src/intrinsics.rs#L1080-L1081">in this comment</a>? I'm pretty sure it's what's causing my local changes to fail to compile the tests.</p>
</blockquote>
<p>that's <a href="https://github.com/rust-lang/rust/blob/47a90f452050d4ea484206447babb07dd33c21d5/compiler/rustc_mir/src/transform/check_consts/validation.rs#L891">this code</a></p>



<a name="240534664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240534664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240534664">(May 27 2021 at 21:30)</a>:</h4>
<p>Won't people inevitable end up relying on the transmute-arithmetic-transmute "flow" if the pointer ops aren't stabilized?</p>



<a name="240534673"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240534673" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240534673">(May 27 2021 at 21:30)</a>:</h4>
<p>And that chunk of code would just need to be removed, correct?</p>



<a name="240536007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240536007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240536007">(May 27 2021 at 21:43)</a>:</h4>
<p>well... any attempt to do pointer ops just errors out <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="240536016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240536016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240536016">(May 27 2021 at 21:43)</a>:</h4>
<p>try it out with the feature gate</p>



<a name="240539969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240539969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240539969">(May 27 2021 at 22:26)</a>:</h4>
<blockquote>
<p>cannot cast pointer to integer because it was not created by cast from integer</p>
</blockquote>
<p>Well I'll be damned <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span> I was <em>not</em> expecting that! Kudos to whoever implemented that loveliness.</p>



<a name="240540007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240540007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240540007">(May 27 2021 at 22:27)</a>:</h4>
<p>In that case I agree, there's no reason to do that at the same time. transmute and union would be the minimum reasonable amount</p>



<a name="240695359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240695359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240695359">(May 29 2021 at 10:29)</a>:</h4>
<p><span class="user-mention silent" data-user-id="245610">Jacob Pratt</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn/near/240539969">said</a>:</p>
<blockquote>
<blockquote>
<p>cannot cast pointer to integer because it was not created by cast from integer</p>
</blockquote>
<p>Well I'll be damned <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span> I was <em>not</em> expecting that! Kudos to whoever implemented that loveliness.</p>
</blockquote>
<p>actually, it would be <em>extra</em> work to make this code work. making integer ops on pointers fail is the easy way.^^</p>



<a name="240695367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240695367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240695367">(May 29 2021 at 10:29)</a>:</h4>
<p>(and FWIW, code doing transmute-arith-transmute might actually be or become UB in LLVM, but that is a separate story)</p>



<a name="240695427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240695427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240695427">(May 29 2021 at 10:30)</a>:</h4>
<p>see <a href="https://rustc-dev-guide.rust-lang.org/miri.html#memory">https://rustc-dev-guide.rust-lang.org/miri.html#memory</a> for details</p>



<a name="240697894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240697894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240697894">(May 29 2021 at 11:39)</a>:</h4>
<p>so, is anyone up for preparing the PR that stabilizes transmute and union accesses in <code>const fn</code>? I'd happily hep with reviews and stuff. :D</p>



<a name="240698155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240698155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240698155">(May 29 2021 at 11:47)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/85769">https://github.com/rust-lang/rust/pull/85769</a></p>



<a name="240698745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240698745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240698745">(May 29 2021 at 12:02)</a>:</h4>
<p>ah, you're faster than light, impressive :)</p>



<a name="240720891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/unions%2C%20transmute%2C%20raw%20ptr%20in%20const%20fn/near/240720891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Pratt <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/unions.2C.20transmute.2C.20raw.20ptr.20in.20const.20fn.html#240720891">(May 29 2021 at 21:01)</a>:</h4>
<p>Yeah I'm working on it slowly. Just minor things here and there that cause issues, really.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>